package com.hongyuan.coach.ui.home.view_mode;

import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.coach.R;
import com.hongyuan.coach.base.CustomViewModel;
import com.hongyuan.coach.data.Constants;
import com.hongyuan.coach.data.MyRepository;
import com.hongyuan.coach.myview.scllor_view.UnitBean;
import com.hongyuan.coach.myview.scllor_view.UnitBeanUtils;
import com.hongyuan.coach.myview.select_time.ContentTimeBean;
import com.hongyuan.coach.ui.home.beans.SelectCoachBeans;
import com.hongyuan.coach.ui.home.beans.SelectCourseBeans;
import com.hongyuan.coach.ui.home.beans.StudentsBeans;
import com.hongyuan.coach.ui.home.beans.TopTimeBean;
import java.util.List;
import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class AppointmentStudentsViewMode extends CustomViewModel<MyRepository> {

    //课程选取工具
    public UnitBeanUtils cpUtils;
    //课程数据集
    private ObservableField<List<SelectCourseBeans>> cpList = new ObservableField<>();
    //已选课程名
    public ObservableField<String> courseName = new ObservableField<>();
    //教练列表选取工具
    public UnitBeanUtils coachUtils;
    //教练数据集
    private ObservableField<List<SelectCoachBeans>> coachList = new ObservableField<>();
    //已选教练名
    public ObservableField<String> coachName = new ObservableField<>();
    //选中的学员集合
    public ObservableField<List<StudentsBeans.ListBean>> stuList = new ObservableField<>();

    public AppointmentStudentsViewMode(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //弹框选择课程
        public SingleLiveEvent<List<UnitBean>> showCps = new SingleLiveEvent<>();
        //弹框选择教练
        public SingleLiveEvent<List<UnitBean>> showCoachs = new SingleLiveEvent<>();
        //设置时间选择器头部数据
        public SingleLiveEvent<List<TopTimeBean>> setTimeTopData = new SingleLiveEvent<>();
        //设置时间选择器内容数据
        public SingleLiveEvent<List<ContentTimeBean>> setTimeContentData = new SingleLiveEvent<>();
        //跳转选择学员
        public SingleLiveEvent<Void> selectStudents = new SingleLiveEvent<>();
        //预约学员
        public SingleLiveEvent<Void> submit = new SingleLiveEvent<>();
    }

    //给RecyclerView添加ObservableList
    public ObservableList<AppointmentStudentsItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<AppointmentStudentsItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_appointment_students);


    //选择课程
    public BindingCommand selectCp = new BindingCommand(() -> {

        if(cpUtils != null && cpList.get() != null && cpList.get().size() > 0){
            uc.showCps.setValue(cpUtils.getUnitList(cpList.get()));
        }else{
            ToastUtils.showShort("暂无可约课程");
        }

    });

    //选择教练
    public BindingCommand selectCoach = new BindingCommand(() -> {

        if(coachUtils != null && coachList.get() != null && coachList.get().size() > 0){
            uc.showCoachs.setValue(coachUtils.getUnitList(coachList.get()));
        }else{
            if("请选择".equals(courseName.get())){
                ToastUtils.showShort("请先选择课程");
            }else{
                ToastUtils.showShort("该课程无教练可选");
            }

        }

    });

    //去选中学生
    public BindingCommand selectStudents = new BindingCommand(() -> {
        uc.selectStudents.call();
    });

    //预约
    public BindingCommand submit = new BindingCommand(() -> {
        if(observableList.size() > 0){
            uc.submit.call();
        }
    });

    /*
    * 查询并显示添加的学员
    * */
    public void findOrAdd(List<StudentsBeans.ListBean> selectList){

        label:for(StudentsBeans.ListBean bean : selectList){

            if(stuList.get().size() > 0){
                for(StudentsBeans.ListBean haveBean : stuList.get()){
                    if(bean.getM_id() == haveBean.getM_id()){
                        //跳出外层循环继续下一次循环
                        continue label;
                    }
                }
            }

            //创建一个ui试图
            AppointmentStudentsItemViewModel itemViewModel = new AppointmentStudentsItemViewModel(this,bean);
            observableList.add(0,itemViewModel);

            //添加到当前所选集合中
            stuList.get().add(0,bean);
        }
    }

    /*
     * 获取学员课程id
     * */
    public String getCourseIdS(){
        String ids = "";
        if(stuList.get() == null || stuList.get().size() <= 0){
            return "";
        }
        for(StudentsBeans.ListBean listBean: stuList.get()){
            for(StudentsBeans.ListBean.CourseListBean courseListBean : listBean.getCourse_list()){
                if(courseListBean.isSelect()){
                    ids += ","+courseListBean.getMy_course_id();
                }
            }
        }
        return ids.substring(1);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.CP_LIST){
            List<SelectCourseBeans> mList = (List<SelectCourseBeans>) dataBean;
            cpList.set(mList);

            cpUtils = new UnitBeanUtils<SelectCourseBeans>() {
                @Override
                public String unit(SelectCourseBeans o) {
                    return String.valueOf(o.getCp_id());
                }

                @Override
                public String unit_cn(SelectCourseBeans o) {
                    return o.getCp_name();
                }
            };

        }

        if(code == Constants.PRIVITE_COACH_LIST){
            List<SelectCoachBeans> mList = (List<SelectCoachBeans>) dataBean;
            coachList.set(mList);

            coachUtils = new UnitBeanUtils<SelectCoachBeans>() {
                @Override
                public String unit(SelectCoachBeans o) {
                    return String.valueOf(o.getM_id());
                }

                @Override
                public String unit_cn(SelectCoachBeans o) {
                    return o.getMi_realname();
                }
            };
        }

        if(code == Constants.GET_PLAN_DATA){
            List<TopTimeBean>  mList = (List<TopTimeBean>) dataBean;
            uc.setTimeTopData.setValue(mList);
        }

        if(code == Constants.GET_COACH_TIMEPLAN_DAY_LIST){
            List<ContentTimeBean> list = (List<ContentTimeBean>) dataBean;
            uc.setTimeContentData.setValue(list);
        }

        if(code == Constants.ADD_COURSE_PRIVITE_APPOINTMENT){
            ToastUtils.showShort("预约成功！");
            finish();
        }
    }

}
